﻿using ConfigInfoWebPlatform.API.BusinessCommon;
using ConfigInfoWebPlatform.API.ViewModel;
using ConfigInfoWebPlatform.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConfigInfoWebPlatform.API.DBCommon
{
    /// <summary>
    /// 组织数据层
    /// </summary>
    public class DAL_Organization
    {
        private IFreeSql freeSql;
        /// <summary>
        /// 组织数据层
        /// </summary>
        public DAL_Organization()
        {
            freeSql = FreesqlFactory.GetFreeSql("configplatform");
        }

        /// <summary>
        /// 获取组织数据
        /// </summary>
        /// <returns></returns>
        public List<OrganizationListModel> GetOrganizationList(string did, bool? isAdmin)
        {
            var query = freeSql.Select<Organization, Role, Department>()
                             .LeftJoin((a, b, c) => a.RId == b.Id)
                             .LeftJoin((a, b, c) => a.DId == c.Id)
                             .WhereIf(isAdmin == false, (a, b, c) => a.DId == did.Replace("-", ""))
                             .ToList((a, b, c) => new OrganizationListModel()
                             {
                                 Id=a.Id,
                                 Rid=a.RId,
                                 Rname=b.Name,
                                 Did=a.DId,
                                 Dname=c.Name,
                                 Name=a.Name
                             });
            return query;
        }

        /// <summary>
        /// 添加组织信息
        /// </summary>
        public int InsertOrganization(OrganizationModel organization)
        {
            Organization model = new Organization();
            model.Id = Snowflake.Instance().GetId().ToString();
            model.DId = organization.Did;
            model.RId = organization.Rid;
            model.Name = organization.Name;
            var result = freeSql.Insert<Organization>(model).ExecuteAffrows();
            return result;
        }


        /// <summary>
        /// 编辑组织信息
        /// </summary>
        public int EditOrganization(OrganizationModel organization)
        {
            Organization model = new Organization();
            model.Id = organization.Id;
            model.DId = organization.Did;
            model.RId = organization.Rid;
            model.Name = organization.Name;
            var result = freeSql.Update<Organization>()
                              .SetSource(model)
                              .ExecuteAffrows();
            return result;
        }

        /// <summary>
        /// 删除组织
        /// </summary>
        public int DeleteOrganization(string id)
        {
            //首先修改组织下人员数据
            var personResult = freeSql.Update<PersonInfo>()
                                    .Set(x => x.GId, "")
                                    .Set(x => x.RId, "")
                                    .Where(x => x.GId == id)
                                    .ExecuteAffrows();
            //删除组织数据
            var result = freeSql.Delete<Organization>()
                              .Where(x => x.Id == id)
                              .ExecuteAffrows();
            return result;
        }

        /// <summary>
        /// 通过部门Id、角色Id、组织名称查看组织是否存在
        /// </summary>
        public bool IsExists(string id="", string did="",string rid="",string name="",bool isRepeat=true)
        {
            var result = freeSql.Select<Organization>()
                              .WhereIf(!string.IsNullOrWhiteSpace(id) && isRepeat, x => x.Id != id)
                              .WhereIf(!string.IsNullOrWhiteSpace(id) && !isRepeat, x => x.Id == id)
                              .WhereIf(!string.IsNullOrWhiteSpace(did), x => x.DId == did)
                              .WhereIf(!string.IsNullOrWhiteSpace(rid), x => x.RId == rid)
                              .WhereIf(!string.IsNullOrWhiteSpace(name), x => x.Name == name)
                              .Count();
            return result > 0 ? true : false;
        }

        /// <summary>
        /// 获取组织详情信息
        /// </summary>
        /// <param name="id">组织Id</param>
        /// <returns></returns>
        public OrganizationModel GetOrganizationDetail(string id)
        {
            var query = freeSql.Select<Organization>()
                             .Where(x => x.Id == id)
                             .ToOne(x => new OrganizationModel()
                             {
                                 Id = x.Id,
                                 Rid = x.RId,
                                 Did = x.DId,
                                 Name = x.Name
                             });
            return query;
        }

        /// <summary>
        /// 获取部门数据
        /// </summary>
        /// <returns></returns>
        public List<DepartmentModel> GetDepartments()
        {
            var query = freeSql.Select<Department>()
                             .ToList(x => new DepartmentModel
                             {
                                 Id = x.Id,
                                 Name = x.Name
                             });
            return query;
        }

        /// <summary>
        /// 根据部门Id获取角色数据
        /// </summary>
        /// <param name="did">部门Id</param>
        /// <returns></returns>
        public List<RoleModel> GetRolesByDid(string did)
        {
            var query = freeSql.Select<Role>()
                             .Where(x => x.DId == did)
                             .ToList(x => new RoleModel
                             {
                                 Id = x.Id,
                                 Name = x.Name
                             });
            return query;
        }

        internal List<PersonModel> GetPersons(string did,string oid)
        {
            var query = freeSql.Select<PersonInfo>()
                             .Where(x => x.DId.Replace("-", "") == did)
                             .ToList(x => new PersonModel
                             {
                                 Id = x.Id,
                                 Name = x.Xianshimingcheng,
                                 Oid = x.GId,
                                 Disabled = (string.IsNullOrWhiteSpace(x.GId) || x.GId == oid) ? false : true
                             });
            return query;
        }

        /// <summary>
        /// 清除人员组织信息
        /// </summary>
        public int ClearPersonInfo(string oid)
        {
            var result = freeSql.Update<PersonInfo>()
                              .Set(x => x.GId, "")
                              .Where(x => x.GId == oid)
                              .ExecuteAffrows();
            return result;
        }

        /// <summary>
        /// 修改人员组织、角色信息
        /// </summary>
        public int EditPersonInfo(string id,string gid,string rid)
        {
            //修改组织下人员数据
            var result = freeSql.Update<PersonInfo>()
                              .Set(x => x.GId, gid)
                              .Where(x => x.Id == id)
                              .ExecuteAffrows();
            return result;
        }

        /// <summary>
        /// 根据角色Id获取对应的菜单Id
        /// </summary>
        public List<string> GetPersonIds(string oid)
        {
            var query = freeSql.Select<PersonInfo>()
                             .Where(x => x.GId == oid)
                             .ToList(x => x.Id);
            return query;
        }

    }
}
